/********************************************************************************
*  This file is part of CinoLib                                                 *
*  Copyright(C) 2016: Marco Livesu                                              *
*                                                                               *
*  The MIT License                                                              *
*                                                                               *
*  Permission is hereby granted, free of charge, to any person obtaining a      *
*  copy of this software and associated documentation files (the "Software"),   *
*  to deal in the Software without restriction, including without limitation    *
*  the rights to use, copy, modify, merge, publish, distribute, sublicense,     *
*  and/or sell copies of the Software, and to permit persons to whom the        *
*  Software is furnished to do so, subject to the following conditions:         *
*                                                                               *
*  The above copyright notice and this permission notice shall be included in   *
*  all copies or substantial portions of the Software.                          *
*                                                                               *
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
*  FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE *
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS *
*  IN THE SOFTWARE.                                                             *
*                                                                               *
*  Author(s):                                                                   *
*                                                                               *
*     Marco Livesu (marco.livesu@gmail.com)                                     *
*     http://pers.ge.imati.cnr.it/livesu/                                       *
*                                                                               *
*     Italian National Research Council (CNR)                                   *
*     Institute for Applied Mathematics and Information Technologies (IMATI)    *
*     Via de Marini, 6                                                          *
*     16149 Genoa,                                                              *
*     Italy                                                                     *
*********************************************************************************/
#include <cinolib/pca.h>
#include <cinolib/covmat.h>

namespace cinolib
{

/* Computes the Principal Component Analysis of a list of points in 3D space.
 * It returns the three mutually orthogonal eigenvectors of the covariance matrix,
 * each one scaled by its corresponding eigenvalue.
*/

CINO_INLINE
void PCA(const std::vector<vec3d> & points, vec3d & e_min, vec3d & e_mid, vec3d & e_max)
{
    double cov[3][3];
    covariance_matrix(points, cov);

    double eval[3];
    double evec[3][3];
    mat_eigendec<3,3,double>(cov, eval, evec);

    // rescale vectors according to eigenvalues
    mat_col<3,3,double>(evec,0,e_min.ptr());
    mat_col<3,3,double>(evec,1,e_mid.ptr());
    mat_col<3,3,double>(evec,2,e_max.ptr());
    e_min *= eval[0];
    e_mid *= eval[1];
    e_max *= eval[2];
}

}
